/*
    http://linglu.ysepan.com/
	更新说明：
	1.1:增加了礼盒发光的特效
	1.2:将原本的源码转换成新语法
	1.3:增加了存档功能以及管理员给予玩家金币的功能
	1.4:增加了坦克死亡给予全体存活幸存者金币
	1.5:更新了部分代码的写法
	1.6:更新了新语法SourceMod 1.11，和存档使用 SQLite 数据库，增加了女巫死亡给予全体存活幸存者金币，菜单回血添加了玩家被特感控制，杀死特感并回满血. -by:豆瓣酱な
*/


#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdktools>
#include <adminmenu>

#define PLUGIN_VERSION "1.6"
#define PLUGIN_FLAG FCVAR_SPONLY|FCVAR_NOTIFY
#define COMMAND_FILTER COMMAND_FILTER_CONNECTED|COMMAND_FILTER_NO_BOTS
#define MDL_GIFT "models/items/l4d_gift.mdl"

ConVar g_cvGiftEnable;
ConVar g_cvGiftLife;
ConVar g_cvGiftChance;
ConVar g_cvGiftCashMin;
ConVar g_cvGiftCashMax;
ConVar g_cvTankCash;
ConVar g_cvWitchCash;
ConVar g_cvCashMaxLimit;

ConVar g_cvItemCash[64];

Database g_dbCashStore;

int g_iSurvivorCash[MAXPLAYERS + 1];
float g_fGifLife[2000];

bool g_bRoundEnd;

enum struct ItemCosts {
    int CostPistol;
    int CostMagnum;
    int CostUzi;
    int CostSMG;
    int CostMP5;
    int CostM16;
    int CostAK47;
    int CostSCAR;
    int CostSG552;
    int CostHunting;
    int CostMilitary;
    int CostAWP;
    int CostScout;
    int CostAuto;
    int CostSPAS;
    int CostChrome;
    int CostPump;
    int CostLauncher;
    int CostM60;
    int CostBat;
    int CostMachete;
    int CostKatana;
    int CostTonfa;
    int CostFireAxe;
    int CostGuitar;
    int CostPan;
    int CostCricketBat;
    int CostChainSaw;
    int CostPipe;
    int CostMolotov;
    int CostBile;
    int CostHealthKit;
    int CostDefib;
    int CostAdren;
    int CostPills;
    int CostExplosiveAmmo;
    int CostFireAmmo;
    int CostLaserSight;
    int CostHeal;
    int CostAmmo;
}

ItemCosts g_itemCosts;

public Plugin myinfo = {
    name = "l4d2_gift_store",
    author = "凌鹿",
    description = "幸存者打死特感有几率掉出礼盒,捡起礼盒获取金币,可以进商店换取物品.",
    version = PLUGIN_VERSION,
    url = "https://gitee.com/sweet-and-sour-bombs/adfvsdsd"
}

public void OnPluginStart()
{
    InitDatabase();
    RegAdminCmd("sm_addcash", Command_AddCash, ADMFLAG_KICK, "管理员设置玩家货币数量.");
    RegAdminCmd("sm_add", Command_AddCash, ADMFLAG_KICK, "管理员设置玩家货币数量.");	
    RegConsoleCmd("sm_buy", MenuFunc_MainMenu, "打开金币商店的菜单.");
    RegConsoleCmd("sm_b", MenuFunc_MainMenu, "打开金币商店的菜单.");
    RegConsoleCmd("sm_rpg", MenuFunc_MainMenu, "打开金币商店的菜单.");	

    CreateConVar("l4d2_gift_version", PLUGIN_VERSION, "插件版本", PLUGIN_FLAG);
    g_cvGiftEnable = CreateConVar("l4d2_gift_enabled", "1", "插件开/关 [1=开|0=关].", PLUGIN_FLAG, true, 0.00, true, 1.00);
    g_cvGiftLife = CreateConVar("l4d2_gift_life", "60.0", "礼物留在地面最大时间.", PLUGIN_FLAG, true, 0.00, true, 9999.00);
    g_cvGiftChance = CreateConVar("l4d2_gift_chance", "33", "特感掉落礼盒的几率(%).", PLUGIN_FLAG, true, 0.00, true, 100.00);
    g_cvGiftCashMin = CreateConVar("l4d2_gift_min", "1", "开启礼盒获取金币最小是多少.", PLUGIN_FLAG, true, 0.00, true, 9999.00);
    g_cvGiftCashMax = CreateConVar("l4d2_gift_max", "3", "开启礼盒获取金币最大是多少.", PLUGIN_FLAG, true, 0.00, true, 9999.00);
    g_cvTankCash = CreateConVar("l4d2_tank_cash", "15", "坦克死亡存活幸存者获得多少金币.", PLUGIN_FLAG, true, 0.00, true, 9999.00);
    g_cvWitchCash = CreateConVar("l4d2_witch_cash", "5", "Witch死亡存活幸存者获得多少金币.", PLUGIN_FLAG, true, 0.00, true, 9999.00);
    g_cvCashMaxLimit = CreateConVar("l4d2_cash_max_limit", "100000000", "总共金币最大上限是多少.", PLUGIN_FLAG, true, 0.00, true, 100000000.00);


    //AutoExecConfig(true, "l4d2_gift_store");

    InitItemCash();

    HookEvent("player_first_spawn", Event_PlayerFirstSpawn);
    HookEvent("player_death", Event_PlayerDeath);
    HookEvent("round_start", Event_RoundStart);
    HookEvent("round_end", Event_RoundEnd);
    HookEvent("tank_killed", Event_TankKilled);
    HookEvent("witch_killed", Event_WitchKilled);
}

void InitDatabase()
{
    char sError[256];
    g_dbCashStore = SQLite_UseDatabase("l4d2_gift_store", sError, sizeof(sError));
    if (g_dbCashStore == null)
    {
        SetFailState("无法连接到数据库: %s", sError);
        return;
    }
    
    // 创建表如果不存在
    char sQuery[512];
    Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS player_cash (steamid TEXT PRIMARY KEY, cash INTEGER);");
    if (!SQL_FastQuery(g_dbCashStore, sQuery))
    {
        SQL_GetError(g_dbCashStore, sError, sizeof(sError));
        SetFailState("无法创建表: %s", sError);
    }
}

void InitItemCash()
{
    g_itemCosts.CostPistol = 1;
    g_itemCosts.CostMagnum = 2;
    g_itemCosts.CostUzi = 3;
    g_itemCosts.CostSMG = 4;
    g_itemCosts.CostMP5 = 5;
    g_itemCosts.CostM16 = 6;
    g_itemCosts.CostAK47 = 7;
    g_itemCosts.CostSCAR = 8;
    g_itemCosts.CostSG552 = 9;
    g_itemCosts.CostHunting = 10;
    g_itemCosts.CostMilitary = 11;
    g_itemCosts.CostScout = 12;
    g_itemCosts.CostAWP = 13;
    g_itemCosts.CostPump = 14;
    g_itemCosts.CostAuto = 15;
    g_itemCosts.CostChrome = 16;
    g_itemCosts.CostSPAS = 17;
    g_itemCosts.CostLauncher = 18;
    g_itemCosts.CostM60 = 19;
    g_itemCosts.CostBat = 20;
    g_itemCosts.CostMachete = 21;
    g_itemCosts.CostKatana = 22;
    g_itemCosts.CostTonfa = 23;
    g_itemCosts.CostFireAxe = 24;
    g_itemCosts.CostGuitar = 25;
    g_itemCosts.CostPan = 26;
    g_itemCosts.CostCricketBat = 27;
    g_itemCosts.CostChainSaw = 28;
    g_itemCosts.CostPipe = 29;
    g_itemCosts.CostMolotov = 30;
    g_itemCosts.CostBile = 31;
    g_itemCosts.CostHealthKit = 32;
    g_itemCosts.CostDefib = 33;
    g_itemCosts.CostAdren = 34;
    g_itemCosts.CostPills = 35;
    g_itemCosts.CostExplosiveAmmo = 36;
    g_itemCosts.CostFireAmmo = 37;
    g_itemCosts.CostLaserSight = 38;
    g_itemCosts.CostHeal = 39;
    g_itemCosts.CostAmmo = 40;

    g_cvItemCash[g_itemCosts.CostPistol] = CreateConVar("l4d2_points_pistol", "5", "购买普通手枪需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostMagnum] = CreateConVar("l4d2_points_magnum", "10", "购买马格南需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostUzi] = CreateConVar("l4d2_points_uzi", "10", "购买UZI需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostSMG] = CreateConVar("l4d2_points_smg", "12", "购买SMG需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostMP5] = CreateConVar("l4d2_points_mp5", "15", "购买MP5需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostM16] = CreateConVar("l4d2_points_m16", "20", "购买M16需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostAK47] = CreateConVar("l4d2_points_ak", "25", "购买AK47需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostSCAR] = CreateConVar("l4d2_points_scar", "22", "购买SCAR需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostSG552] = CreateConVar("l4d2_points_sg", "25", "购买SG552需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostHunting] = CreateConVar("l4d2_points_hunting_rifle", "20", "购买15发狙击枪需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostMilitary] = CreateConVar("l4d2_points_military_sniper", "25", "购买30发狙击枪需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostScout] = CreateConVar("l4d2_points_scout", "20", "购买鸟狙需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostAWP] = CreateConVar("l4d2_points_awp", "30", "购买AWP需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostPump] = CreateConVar("l4d2_points_pump", "10", "购买一代单喷需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostAuto] = CreateConVar("l4d2_points_autoshotgun", "20", "购买一代连喷需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostChrome] = CreateConVar("l4d2_points_chrome", "10", "购买二代单喷需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostSPAS] = CreateConVar("l4d2_points_spas", "20", "购买二代连喷需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostLauncher] = CreateConVar("l4d2_points_grenade", "30", "购买榴弹发射器需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostM60] = CreateConVar("l4d2_points_m60", "30", "购买M60需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostBat] = CreateConVar("l4d2_points_bat", "5", "购买棒球棍需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostMachete] = CreateConVar("l4d2_points_machete", "10", "购买砍刀需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostKatana] = CreateConVar("l4d2_points_katana", "10", "购买武士刀需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostTonfa] = CreateConVar("l4d2_points_tonfa", "5", "购买警棍需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostFireAxe] = CreateConVar("l4d2_points_fireaxe", "8", "购买消防斧需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostGuitar] = CreateConVar("l4d2_points_guitar", "8", "购买吉他需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostPan] = CreateConVar("l4d2_points_pan", "5", "购买平底锅需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostCricketBat] = CreateConVar("l4d2_points_cricketbat", "5", "购买板球拍需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostChainSaw] = CreateConVar("l4d2_points_chainsaw", "8", "购买电锯需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostPipe] = CreateConVar("l4d2_points_pipe", "15", "购买土制炸弹需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostMolotov] = CreateConVar("l4d2_points_molotov", "15", "购买燃烧瓶需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostBile] = CreateConVar("l4d2_points_bile", "15", "购买胆汁瓶需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostHealthKit] = CreateConVar("l4d2_points_kit", "35", "购买医疗包需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostDefib] = CreateConVar("l4d2_points_defib", "30", "购买电击器需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostAdren] = CreateConVar("l4d2_points_adrenaline", "20", "购买肾上腺素需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostPills] = CreateConVar("l4d2_points_pills", "20", "购买止痛药需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostExplosiveAmmo] = CreateConVar("l4d2_points_explosive_ammo", "15", "购买高爆弹需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostFireAmmo] = CreateConVar("l4d2_points_incendiary_ammo", "15", "购买燃烧弹需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostLaserSight] = CreateConVar("l4d2_points_laser", "10", "购买红外线需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostHeal] = CreateConVar("l4d2_points_survivor_heal", "50", "购买回满血需要多少金币.", PLUGIN_FLAG);
    g_cvItemCash[g_itemCosts.CostAmmo] = CreateConVar("l4d2_points_refill", "10", "购买补满子弹需要多少金币.", PLUGIN_FLAG);
}

public void OnMapStart()
{
    PrecacheModel(MDL_GIFT);
}

public void OnClientPutInServer(int client)
{
    if(!IsFakeClient(client))
    {
        g_iSurvivorCash[client] = 0;
        LoadClientCash(client);
    }
}

public void OnClientDisconnect(int client)
{
    if(!IsFakeClient(client))
    {
        SaveClientCash(client);
        g_iSurvivorCash[client] = 0;
    }
}

void LoadClientCash(int client)
{
    if (g_dbCashStore == null || !IsClientInGame(client) || IsFakeClient(client))
        return;

    char steamId[32];
    if (!GetClientAuthId(client, AuthId_Steam2, steamId, sizeof(steamId)))
    {
        LogError("无法获取玩家的SteamID");
        return;
    }

    char query[256];
    Format(query, sizeof(query), "SELECT cash FROM player_cash WHERE steamid = '%s';", steamId);
    
    g_dbCashStore.Query(SQL_LoadClientCashCallback, query, GetClientUserId(client));
}

public void SQL_LoadClientCashCallback(Database db, DBResultSet results, const char[] error, any data)
{
    if (db == null || results == null)
    {
        LogError("数据库查询错误: %s", error);
        return;
    }

    int client = GetClientOfUserId(data);
    if (client == 0)
        return;

    if (results.FetchRow())
    {
        g_iSurvivorCash[client] = results.FetchInt(0);
    }
    else
    {
        // 玩家没有记录，创建新记录
        char steamId[32];
        if (!GetClientAuthId(client, AuthId_Steam2, steamId, sizeof(steamId)))
            return;

        char query[256];
        Format(query, sizeof(query), "INSERT INTO player_cash (steamid, cash) VALUES ('%s', 0);", steamId);
        g_dbCashStore.Query(SQL_CheckForErrors, query);
    }
}

void SaveClientCash(int client)
{
    if (g_dbCashStore == null || !IsClientInGame(client) || IsFakeClient(client))
        return;

    char steamId[32];
    if (!GetClientAuthId(client, AuthId_Steam2, steamId, sizeof(steamId)))
    {
        LogError("无法获取玩家的SteamID");
        return;
    }

    char query[256];
    Format(query, sizeof(query), "UPDATE OR IGNORE player_cash SET cash = %d WHERE steamid = '%s';", 
        g_iSurvivorCash[client], steamId);
    
    g_dbCashStore.Query(SQL_CheckForErrors, query);
}

public void SQL_CheckForErrors(Database db, DBResultSet results, const char[] error, any data)
{
    if (db == null || results == null)
    {
        LogError("数据库错误: %s", error);
    }
}

public Action Command_AddCash(int client, int args)
{
    Menu_AddCashMenu_Select(client);
    return Plugin_Handled;
}

void Menu_AddCashMenu_Select(int client)
{
    Menu menu = new Menu(Handler_AddCashMenu_Select);    
    menu.SetTitle("选择玩家:");
    menu.ExitBackButton = true;
    AddTargetsToMenu2(menu, client, COMMAND_FILTER);
    menu.Display(client, MENU_TIME_FOREVER);
}

public int Handler_AddCashMenu_Select(Menu menu, MenuAction action, int client, int item)
{
    if (action == MenuAction_End)    
        delete menu;
    else if (action == MenuAction_Select)
    {
        char info[32];
        int userid, target;

        menu.GetItem(item, info, sizeof(info));
        userid = StringToInt(info);
        target = GetClientOfUserId(userid);
        Menu_AddCashMenu(client, target);
    }
    return 0;    
}

void Menu_AddCashMenu(int client, int target)
{
    Menu menu = new Menu(Handler_AddCachMenu);
    char targetinfo[16];
    menu.SetTitle("给予玩家金币数量:");
    IntToString(target, targetinfo, sizeof(targetinfo));
    menu.AddItem(targetinfo, "1");
    menu.AddItem(targetinfo, "5");
    menu.AddItem(targetinfo, "10");
    menu.AddItem(targetinfo, "50");
    menu.AddItem(targetinfo, "100");
    menu.AddItem(targetinfo, "200");
    menu.ExitBackButton = true;
    menu.Display(client, MENU_TIME_FOREVER);    
}

public int Handler_AddCachMenu(Menu menu, MenuAction action, int client, int item)
{
    if (action == MenuAction_End)
        delete menu;
    else if (action == MenuAction_Select)
    {
        int cash = 0;
        switch(item)
        {
            case 0: cash = 1;
            case 1: cash = 5;
            case 2: cash = 10;
            case 3: cash = 50;
            case 4: cash = 100;
            case 5: cash = 200;
        }
        g_iSurvivorCash[client] += cash;
        PrintToChatAll("\x05[提示] \x03管理员给予玩家 \x05%N \x03%d \x05金币.", client, cash);

        if(g_iSurvivorCash[client] >= g_cvCashMaxLimit.IntValue)
        {
            g_iSurvivorCash[client] = g_cvCashMaxLimit.IntValue;
            PrintToChat(client, "\x05[提示] \x03你的金币已达上限，请尽快使用掉.");
        }
    }
    return 0;    
}

public Action MenuFunc_MainMenu(int client, int args)
{
    if (!IsPlayerAlive(client) || GetClientTeam(client) != 2)
    {
        PrintToChat(client, "你已经死亡了或你不是幸存者!");
        return Plugin_Handled;
    }

    Menu menu = new Menu(MenuHandler_MainMenu);
    char line[1024];

    Format(line, sizeof(line), "****************\n*——金币商城——*\n****************\n你的金币为 %i 个.", g_iSurvivorCash[client]);
    menu.SetTitle(line);

    menu.AddItem("item0", "购买枪械武器");
    menu.AddItem("item1", "购买近战武器");
    menu.AddItem("item2", "购买药物弹药");
    menu.AddItem("item3", "金币获取途径");

    menu.ExitButton = true;
    menu.Display(client, MENU_TIME_FOREVER);
    return Plugin_Handled;
}

public int MenuHandler_MainMenu(Menu menu, MenuAction action, int client, int item)
{
    if (action == MenuAction_End)    
        delete menu;

    if(action == MenuAction_Select)
    {
        switch(item)
        {
            case 0: MenuFunc_BuyGun(client);
            case 1: MenuFunc_BuyMelee(client);
            case 2: MenuFunc_BuyMedicine(client);
            case 3: MenuFunc_GetCash(client);
        }
    }
    return 0;    
}

public Action MenuFunc_BuyGun(int client)
{
    Menu menu = new Menu(MenuHandler_BuyGun);
    char line[1024];

    Format(line, sizeof(line), "****************\n*——金币商城——*\n****************\n你的金币为 %i 个.\n选择枪械武器:", g_iSurvivorCash[client]);
    menu.SetTitle(line);

    Format(line,sizeof(line),"普通手枪(%d金币)",g_cvItemCash[g_itemCosts.CostPistol].IntValue);
    menu.AddItem("item0", line);
                    
    Format(line,sizeof(line),"马格南手枪(%d金币)",g_cvItemCash[g_itemCosts.CostMagnum].IntValue);
    menu.AddItem("item1", line);
                
    Format(line,sizeof(line),"UZI冲锋枪(%d金币)",g_cvItemCash[g_itemCosts.CostUzi].IntValue);
    menu.AddItem("item2", line);
                    
    Format(line,sizeof(line),"SMG冲锋枪(%d金币)",g_cvItemCash[g_itemCosts.CostSMG].IntValue);
    menu.AddItem("item3", line);
                    
    Format(line,sizeof(line),"MP5冲锋枪(%d金币)",g_cvItemCash[g_itemCosts.CostMP5].IntValue);
    menu.AddItem("item4", line);
                    
    Format(line,sizeof(line),"一代单发霰弹枪(%d金币)",g_cvItemCash[g_itemCosts.CostPump].IntValue);
    menu.AddItem("item5", line);
                    
    Format(line,sizeof(line),"二代单发霰弹枪(%d金币)",g_cvItemCash[g_itemCosts.CostChrome].IntValue);
    menu.AddItem("item6", line);
                    
    Format(line,sizeof(line),"一代连发霰弹枪(%d金币)",g_cvItemCash[g_itemCosts.CostAuto].IntValue);
    menu.AddItem("item7", line);
                    
    Format(line,sizeof(line),"二代连发霰弹枪(%d金币)",g_cvItemCash[g_itemCosts.CostSPAS].IntValue);
    menu.AddItem("item8", line);
                    
    Format(line,sizeof(line),"一代狙击枪(%d金币)",g_cvItemCash[g_itemCosts.CostHunting].IntValue);
    menu.AddItem("item9", line);
                    
    Format(line,sizeof(line),"二代狙击枪(%d金币)",g_cvItemCash[g_itemCosts.CostMilitary].IntValue);
    menu.AddItem("item10", line);
                    
    Format(line,sizeof(line),"鸟狙狙击枪(%d金币)",g_cvItemCash[g_itemCosts.CostScout].IntValue);
    menu.AddItem("item11", line);
                    
    Format(line,sizeof(line),"AWP狙击枪(%d金币)",g_cvItemCash[g_itemCosts.CostAWP].IntValue);
    menu.AddItem("item12", line);
            
    Format(line,sizeof(line),"M16步枪(%d金币)",g_cvItemCash[g_itemCosts.CostM16].IntValue);
    menu.AddItem("item13", line);
            
    Format(line,sizeof(line),"AK47步枪(%d金币)",g_cvItemCash[g_itemCosts.CostAK47].IntValue);
    menu.AddItem("item14", line);
            
    Format(line,sizeof(line),"SCAR步枪(%d金币)",g_cvItemCash[g_itemCosts.CostSCAR].IntValue);
    menu.AddItem("item15", line);
            
    Format(line,sizeof(line),"SG552步枪(%d金币)",g_cvItemCash[g_itemCosts.CostSG552].IntValue);
    menu.AddItem("item16", line);

    Format(line,sizeof(line),"M60机关枪(%d金币)",g_cvItemCash[g_itemCosts.CostM60].IntValue);
    menu.AddItem("item17", line);
            
    Format(line,sizeof(line),"榴弹发射器(%d金币)",g_cvItemCash[g_itemCosts.CostLauncher].IntValue);
    menu.AddItem("item18", line);

    menu.ExitButton = true;
    menu.Display(client, MENU_TIME_FOREVER);
    return Plugin_Handled;
}

public Action MenuFunc_BuyMelee(int client)
{
    Menu menu = new Menu(MenuHandler_BuyMelee);
    char line[1024];

    Format(line, sizeof(line), "****************\n*——金币商城——*\n****************\n你的金币为 %i 个.\n选择近战武器:", g_iSurvivorCash[client]);
    menu.SetTitle(line);

    Format(line,sizeof(line),"消防斧(%d金币)",g_cvItemCash[g_itemCosts.CostFireAxe].IntValue);
    menu.AddItem("item0", line);
            
    Format(line,sizeof(line),"武士刀(%d金币)",g_cvItemCash[g_itemCosts.CostKatana].IntValue);
    menu.AddItem("item1", line);
            
    Format(line,sizeof(line),"板球拍(%d金币)",g_cvItemCash[g_itemCosts.CostCricketBat].IntValue);
    menu.AddItem("item2", line);
            
    Format(line,sizeof(line),"棒球棍(%d金币)",g_cvItemCash[g_itemCosts.CostBat].IntValue);
    menu.AddItem("item3", line);
            
    Format(line,sizeof(line),"平底锅(%d金币)",g_cvItemCash[g_itemCosts.CostPan].IntValue);
    menu.AddItem("item4", line);
            
    Format(line,sizeof(line),"电吉他(%d金币)",g_cvItemCash[g_itemCosts.CostGuitar].IntValue);
    menu.AddItem("item5", line);
            
    Format(line,sizeof(line),"警棍(%d金币)",g_cvItemCash[g_itemCosts.CostTonfa].IntValue);
    menu.AddItem("item6", line);
            
    Format(line,sizeof(line),"砍刀(%d金币)",g_cvItemCash[g_itemCosts.CostMachete].IntValue);
    menu.AddItem("item7", line);
            
    Format(line,sizeof(line),"电锯(%d金币)",g_cvItemCash[g_itemCosts.CostChainSaw].IntValue);
    menu.AddItem("item8", line);

    menu.ExitButton = true;
    menu.Display(client, MENU_TIME_FOREVER);
    return Plugin_Handled;
}

public Action MenuFunc_BuyMedicine(int client)
{
    Menu menu = new Menu(MenuHandler_BuyMedicine);
    char line[1024];

    Format(line, sizeof(line), "****************\n*——金币商城——*\n****************\n你的金币为 %i 个.\n选择道具弹药:", g_iSurvivorCash[client]);
    menu.SetTitle(line);
            
    Format(line,sizeof(line),"弹药(%d金币)",g_cvItemCash[g_itemCosts.CostAmmo].IntValue);
    menu.AddItem("item0", line);

    Format(line,sizeof(line),"回满血(%d金币)",g_cvItemCash[g_itemCosts.CostHeal].IntValue);
    menu.AddItem("item1", line);
            
    Format(line,sizeof(line),"肾上腺素(%d金币)",g_cvItemCash[g_itemCosts.CostAdren].IntValue);
    menu.AddItem("item2", line);
            
    Format(line,sizeof(line),"止痛药(%d金币)",g_cvItemCash[g_itemCosts.CostPills].IntValue);
    menu.AddItem("item3", line);
            
    Format(line,sizeof(line),"医疗包(%d金币)",g_cvItemCash[g_itemCosts.CostHealthKit].IntValue);
    menu.AddItem("item4", line);
            
    Format(line,sizeof(line),"电击器(%d金币)",g_cvItemCash[g_itemCosts.CostDefib].IntValue);
    menu.AddItem("item5", line);
            
    Format(line,sizeof(line),"土制炸弹(%d金币)",g_cvItemCash[g_itemCosts.CostPipe].IntValue);
    menu.AddItem("item6", line);
            
    Format(line,sizeof(line),"胆汁瓶(%d金币)",g_cvItemCash[g_itemCosts.CostBile].IntValue);
    menu.AddItem("item7", line);
            
    Format(line,sizeof(line),"燃烧瓶(%d金币)",g_cvItemCash[g_itemCosts.CostMolotov].IntValue);
    menu.AddItem("item8", line);
            
    Format(line,sizeof(line),"燃烧弹药箱(%d金币)",g_cvItemCash[g_itemCosts.CostFireAmmo].IntValue);
    menu.AddItem("item9", line);
            
    Format(line,sizeof(line),"高爆弹药箱(%d金币)",g_cvItemCash[g_itemCosts.CostExplosiveAmmo].IntValue);
    menu.AddItem("item10", line);
            
    Format(line,sizeof(line),"红外线装置(%d金币)",g_cvItemCash[g_itemCosts.CostLaserSight].IntValue);
    menu.AddItem("item11", line);

    menu.ExitButton = true;
    menu.Display(client, MENU_TIME_FOREVER);
    return Plugin_Handled;
}

public Action MenuFunc_GetCash(int client)
{
    Menu menu = new Menu(MenuHandler_GetCash);
    char line[1024];

    Format(line, sizeof(line), "****************\n*——帮助界面——*\n****************\n击杀特感有几率掉落礼盒，捡起礼盒获取随机数量金币。");
    menu.SetTitle(line);

    Format(line,sizeof(line),"返回");
    menu.AddItem("item0", line);

    menu.ExitButton = true;
    menu.Display(client, MENU_TIME_FOREVER);
    return Plugin_Handled;
}

public int MenuHandler_BuyGun(Menu menu, MenuAction action, int client, int item)
{
    if (action == MenuAction_End)
        delete menu;

    if(action == MenuAction_Select)
    {
        switch(item)
        {
            case 0:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostPistol].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostPistol].IntValue;
                    CheatCommand(client, "give","pistol");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04普通手枪\x03.", g_cvItemCash[g_itemCosts.CostPistol].IntValue);
                }
            }
            case 1:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostMagnum].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostMagnum].IntValue;
                    CheatCommand(client, "give","pistol_magnum");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04马格南手枪\x03.", g_cvItemCash[g_itemCosts.CostMagnum].IntValue);
                }
            }
            case 2:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostUzi].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostUzi].IntValue;
                    CheatCommand(client, "give","smg");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04UZI冲锋枪\x03.", g_cvItemCash[g_itemCosts.CostUzi].IntValue);
                }
            }
            case 3:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostSMG].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostSMG].IntValue;
                    CheatCommand(client, "give","smg_silenced");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04SMG冲锋枪\x03.", g_cvItemCash[g_itemCosts.CostSMG].IntValue);
                }
            }
            case 4:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostMP5].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostMP5].IntValue;
                    CheatCommand(client, "give","weapon_smg_mp5");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04MP5冲锋枪\x03.", g_cvItemCash[g_itemCosts.CostMP5].IntValue);
                }
            }
            case 5:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostPump].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostPump].IntValue;
                    CheatCommand(client, "give","pumpshotgun");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04一代单发霰弹枪\x03.", g_cvItemCash[g_itemCosts.CostPump].IntValue);
                }
            }
            case 6:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostChrome].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostChrome].IntValue;
                    CheatCommand(client, "give","shotgun_chrome");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04二代单发霰弹枪\x03.", g_cvItemCash[g_itemCosts.CostChrome].IntValue);
                }
            }
            case 7:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostAuto].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostAuto].IntValue;
                    CheatCommand(client, "give","autoshotgun");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04一代连发霰弹枪\x03.", g_cvItemCash[g_itemCosts.CostAuto].IntValue);
                }
            }
            case 8:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostSPAS].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostSPAS].IntValue;
                    CheatCommand(client, "give","shotgun_spas");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04二代连发霰弹枪\x03.", g_cvItemCash[g_itemCosts.CostSPAS].IntValue);
                }
            }
            case 9:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostHunting].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostHunting].IntValue;
                    CheatCommand(client, "give","hunting_rifle");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04一代狙击枪\x03.", g_cvItemCash[g_itemCosts.CostHunting].IntValue);
                }
            }
            case 10:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostMilitary].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostMilitary].IntValue;
                    CheatCommand(client, "give","sniper_military");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04二代狙击枪\x03.", g_cvItemCash[g_itemCosts.CostMilitary].IntValue);
                }
            }
            case 11:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostScout].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostScout].IntValue;
                    CheatCommand(client, "give","weapon_sniper_scout");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x0鸟狙狙击枪\x03.", g_cvItemCash[g_itemCosts.CostScout].IntValue);
                }
            }
            case 12:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostAWP].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostAWP].IntValue;
                    CheatCommand(client, "give","weapon_sniper_awp");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04AWP狙击枪\x03.", g_cvItemCash[g_itemCosts.CostAWP].IntValue);
                }
            }
            case 13:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostM16].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostM16].IntValue;
                    CheatCommand(client, "give","rifle");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04M16步枪\x03.", g_cvItemCash[g_itemCosts.CostM16].IntValue);
                }
            }
            case 14:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostAK47].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostAK47].IntValue;
                    CheatCommand(client, "give","rifle_ak47");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04AK47步枪\x03.", g_cvItemCash[g_itemCosts.CostAK47].IntValue);
                }
            }
            case 15:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostSCAR].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostSCAR].IntValue;
                    CheatCommand(client, "give","rifle_desert");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04SCAR步枪\x03.", g_cvItemCash[g_itemCosts.CostSCAR].IntValue);
                }
            }
            case 16:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostSG552].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostSG552].IntValue;
                    CheatCommand(client, "give","weapon_rifle_sg552");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04SG552步枪\x03.", g_cvItemCash[g_itemCosts.CostSG552].IntValue);
                }
            }
            case 17:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostM60].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostM60].IntValue;
                    CheatCommand(client, "give","rifle_m60");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04M60机关枪\x03.", g_cvItemCash[g_itemCosts.CostM60].IntValue);
                }
            }
            case 18:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostLauncher].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostLauncher].IntValue;
                    CheatCommand(client, "give","weapon_grenade_launcher");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04榴弹发射器\x03.", g_cvItemCash[g_itemCosts.CostLauncher].IntValue);
                }
            }
        }
    }
    return 0;    
}

public int MenuHandler_BuyMelee(Menu menu, MenuAction action, int client, int item)
{
    if (action == MenuAction_End)
        delete menu;

    if(action == MenuAction_Select)
    {
        switch(item)
        {
            case 0:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostFireAxe].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostFireAxe].IntValue;
                    CheatCommand(client, "give","fireaxe");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04消防斧\x03.", g_cvItemCash[g_itemCosts.CostFireAxe].IntValue);
                }
            }
            case 1:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostKatana].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostKatana].IntValue;
                    CheatCommand(client, "give","katana");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x0武士刀\x03.", g_cvItemCash[g_itemCosts.CostKatana].IntValue);
                }
            }
            case 2:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostCricketBat].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostCricketBat].IntValue;
                    CheatCommand(client, "give","cricket_bat");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04板球拍\x03.", g_cvItemCash[g_itemCosts.CostCricketBat].IntValue);
                }
            }
            case 3:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostBat].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostBat].IntValue;
                    CheatCommand(client, "give","baseball_bat");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04棒球棍\x03.", g_cvItemCash[g_itemCosts.CostBat].IntValue);
                }
            }
            case 4:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostPan].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostPan].IntValue;
                    CheatCommand(client, "give","frying_pan");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04平底锅\x03.", g_cvItemCash[g_itemCosts.CostPan].IntValue);
                }
            }
            case 5:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostGuitar].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostGuitar].IntValue;
                    CheatCommand(client, "give","electric_guitar");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04电吉他\x03.", g_cvItemCash[g_itemCosts.CostGuitar].IntValue);
                }
            }
            case 6:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostTonfa].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostTonfa].IntValue;
                    CheatCommand(client, "give","tonfa");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04警棍\x03.", g_cvItemCash[g_itemCosts.CostTonfa].IntValue);
                }
            }
            case 7:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostMachete].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostMachete].IntValue;
                    CheatCommand(client, "give","machete");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04砍刀\x03.", g_cvItemCash[g_itemCosts.CostMachete].IntValue);
                }
            }
            case 8:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostChainSaw].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostChainSaw].IntValue;
                    CheatCommand(client, "give","chainsaw");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04电锯\x03.", g_cvItemCash[g_itemCosts.CostChainSaw].IntValue);
                }
            }
        }
    }
    return 0;    
}

public int MenuHandler_BuyMedicine(Menu menu, MenuAction action, int client, int item)
{
    if (action == MenuAction_End)
        delete menu;

    if(action == MenuAction_Select)
    {
        switch(item)
        {
            case 0:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostAmmo].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostAmmo].IntValue;
                    CheatCommand(client, "give","ammo");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04弹药\x03.", g_cvItemCash[g_itemCosts.CostAmmo].IntValue);
                }
            }
            case 1:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostHeal].IntValue) 
                {
                    PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                }
                else
                {
                    // 检查玩家是否被特感控制
                    int attacker = GetEntPropEnt(client, Prop_Send, "m_pounceAttacker");
                    if (attacker <= 0) attacker = GetEntPropEnt(client, Prop_Send, "m_tongueOwner");
                    if (attacker <= 0) attacker = GetEntPropEnt(client, Prop_Send, "m_pummelAttacker");
                    if (attacker <= 0) attacker = GetEntPropEnt(client, Prop_Send, "m_carryAttacker");
                    if (attacker <= 0) attacker = GetEntPropEnt(client, Prop_Send, "m_jockeyAttacker");
                    
                    if (attacker > 0 && IsClientInGame(attacker) && IsPlayerAlive(attacker))
                    {
                        // 杀死控制玩家的特感
                        ForcePlayerSuicide(attacker);
                        PrintToChatAll("\x05[提示] \x03%N \x05花费\x03%d\x05金币杀死了控制他的特感并回满血!", client, g_cvItemCash[g_itemCosts.CostHeal].IntValue);
                    }
                    else
                    {
                        PrintToChat(client,"\x03你花费了\x04%i\x03金币回满血\x03.", g_cvItemCash[g_itemCosts.CostHeal].IntValue);
                    }
                    
                    // 回满血
                    SetEntProp(client, Prop_Send, "m_iHealth", 100);
                    SetEntPropFloat(client, Prop_Send, "m_healthBuffer", 0.0);
                    
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostHeal].IntValue;
                }
            }
            case 2:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostAdren].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostAdren].IntValue;
                    CheatCommand(client, "give","adrenaline");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04肾上腺素\x03.", g_cvItemCash[g_itemCosts.CostAdren].IntValue);
                }
            }
            case 3:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostPills].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostPills].IntValue;
                    CheatCommand(client, "give","pain_pills");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04止痛药\x03.", g_cvItemCash[g_itemCosts.CostPills].IntValue);
                }
            }
            case 4:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostHealthKit].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostHealthKit].IntValue;
                    CheatCommand(client, "give","first_aid_kit");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04医疗包\x03.", g_cvItemCash[g_itemCosts.CostHealthKit].IntValue);
                }
            }
            case 5:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostDefib].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostDefib].IntValue;
                    CheatCommand(client, "give","defibrillator");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04电击器\x03.", g_cvItemCash[g_itemCosts.CostDefib].IntValue);
                }
            }
            case 6:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostPipe].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostPipe].IntValue;
                    CheatCommand(client, "give","pipe_bomb");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04土制炸弹\x03.", g_cvItemCash[g_itemCosts.CostPipe].IntValue);
                }
            }
            case 7:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostBile].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostBile].IntValue;
                    CheatCommand(client, "give","vomitjar");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04胆汁瓶\x03.", g_cvItemCash[g_itemCosts.CostBile].IntValue);
                }
            }
            case 8:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostMolotov].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostMolotov].IntValue;
                    CheatCommand(client, "give","molotov");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04燃烧瓶\x03.", g_cvItemCash[g_itemCosts.CostMolotov].IntValue);
                }
            }
            case 9:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostFireAmmo].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostFireAmmo].IntValue;
                    CheatCommand(client, "give","weapon_upgradepack_incendiary");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04燃烧弹药箱\x03.", g_cvItemCash[g_itemCosts.CostFireAmmo].IntValue);
                }
            }
            case 10:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostExplosiveAmmo].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostExplosiveAmmo].IntValue;
                    CheatCommand(client, "give","weapon_upgradepack_explosive");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04高爆弹药箱\x03.", g_cvItemCash[g_itemCosts.CostExplosiveAmmo].IntValue);
                }
            }
            case 11:
            {
                if(g_iSurvivorCash[client] < g_cvItemCash[g_itemCosts.CostLaserSight].IntValue) PrintToChat(client,"\x03金币\x04不足\x03以购买!");
                else
                {
                    g_iSurvivorCash[client] -= g_cvItemCash[g_itemCosts.CostLaserSight].IntValue;
                    CheatCommand(client, "upgrade_add","laser_sight");
                    PrintToChat(client,"\x03你花了\x04%i\x03金币购买了\x04红外线装置\x03.", g_cvItemCash[g_itemCosts.CostLaserSight].IntValue);
                }
            }
        }
    }
    return 0;    
}

public int MenuHandler_GetCash(Menu menu, MenuAction action, int client, int item)
{
    if (action == MenuAction_End)
        delete menu;
    if(action == MenuAction_Select && item == 0) MenuFunc_MainMenu(client, 0);
    return 0;
}

public Action Event_PlayerFirstSpawn(Event event, const char[] name, bool dontBroadcast)
{
    int target = GetClientOfUserId(event.GetInt("userid"));
    if(!IsFakeClient(target) && IsClientInGame(target))
        PrintToChat(target, "\x05[提示]\x03在聊天框输入 \x05 !buy \x03 打开商店菜单.");
    return Plugin_Continue;
}

public void Event_PlayerDeath(Event event, const char[] name, bool dontBroadcast)
{
    if (g_cvGiftEnable.IntValue == 0) return;

    int victim = GetClientOfUserId(event.GetInt("userid"));
    int attacker = GetClientOfUserId(event.GetInt("attacker"));

    if (IsValidClient(victim) && GetClientTeam(victim) == 3 && IsValidClient(attacker) && GetClientTeam(attacker) == 2)
    {
        if (GetRandomInt(1, 100) <= g_cvGiftChance.IntValue) DropGift(victim);
    }
}

public void Event_RoundStart(Event event, const char[] name, bool dontBroadcast)
{
    if (g_cvGiftEnable.IntValue == 0) return;

    g_bRoundEnd = false;
}

public void Event_RoundEnd(Event event, const char[] name, bool dontBroadcast)
{
    if (g_cvGiftEnable.IntValue == 0) return;

    g_bRoundEnd = true;
}

public void Event_TankKilled(Event event, const char[] name, bool dontBroadcast)
{
    if (g_cvGiftEnable.IntValue == 0) return;

    for (int client = 1; client <= MaxClients; client++)
    {
        if (IsValidClient(client) && IsPlayerAlive(client) && !IsFakeClient(client) && GetClientTeam(client) == 2)
        {
            g_iSurvivorCash[client] += g_cvTankCash.IntValue;

            if(g_iSurvivorCash[client] >= g_cvCashMaxLimit.IntValue)
            {
                g_iSurvivorCash[client] = g_cvCashMaxLimit.IntValue;
                PrintToChat(client, "\x05[提示] \x03你的金币已达上限，请尽快使用掉.");
            }
        }
    }
    PrintToChatAll("\x05[提示] \x03坦克死亡，全体存活的幸存者获得 \x03%d \x05金币.", g_cvTankCash.IntValue);
}

public void Event_WitchKilled(Event event, const char[] name, bool dontBroadcast)
{
    if (g_cvGiftEnable.IntValue == 0) return;

    int killer = GetClientOfUserId(event.GetInt("userid"));
    
    if (IsValidClient(killer) && GetClientTeam(killer) == 2)
    {
        for (int client = 1; client <= MaxClients; client++)
        {
            if (IsValidClient(client) && !IsFakeClient(client) && GetClientTeam(client) == 2)
            {
                g_iSurvivorCash[client] += g_cvWitchCash.IntValue;

                if(g_iSurvivorCash[client] >= g_cvCashMaxLimit.IntValue)
                {
                    g_iSurvivorCash[client] = g_cvCashMaxLimit.IntValue;
                    PrintToChat(client, "\x05[提示] \x03你的金币已达上限，请尽快使用掉.");
                }
            }
        }
        PrintToChatAll("\x05[提示] \x03女巫死亡，全体幸存者获得 \x03%d \x05金币.", g_cvWitchCash.IntValue);
    }
}

void DropGift(int client)
{
    float GifPos[3];
    GetEntPropVector(client, Prop_Send, "m_vecOrigin", GifPos);
    GifPos[2] += 10.0;

    int gift = CreateEntityByName("prop_dynamic_override");
    if(gift != -1)
    {
        DispatchKeyValue(gift, "model", MDL_GIFT);
        DispatchKeyValueVector(gift, "origin", GifPos);
        SetEntPropFloat(gift, Prop_Send,"m_flModelScale", 1.0);
        
        DispatchSpawn(gift);
        
        g_fGifLife[gift] = 0.0;
        CreateTimer(0.1, Timer_GiftLife, EntIndexToEntRef(gift), TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
    }
}

public Action Timer_GiftLife(Handle timer, int index)
{
    int gift = EntRefToEntIndex(index);
    if (IsValidEntity(gift))
    {
        g_fGifLife[gift] += 0.1;
        if(g_bRoundEnd || g_fGifLife[gift] > g_cvGiftLife.FloatValue)
        {
            g_fGifLife[gift] = 0.0;
            AcceptEntityInput(gift, "kill");
            return Plugin_Stop;
        }

        SetEntProp(gift, Prop_Send, "m_iGlowType", 3);
        SetEntProp(gift, Prop_Send, "m_glowColorOverride", 65280);//绿色
        SetEntProp(gift, Prop_Send, "m_nGlowRange", 1000);

        RotateAdvance(gift, 15.0, 1);

        float PlayerPos[3];
        float GiftPos[3];
        GetEntPropVector(gift, Prop_Send, "m_vecOrigin", GiftPos);

        for (int client = 1; client <= MaxClients; client++)
        {
            if (IsValidClient(client) && IsPlayerAlive(client) && !IsFakeClient(client) && GetClientTeam(client) == 2)
            {
                GetEntPropVector(client, Prop_Send, "m_vecOrigin", PlayerPos);
                if (GetVectorDistance(PlayerPos, GiftPos) < 70.0)
                {
                    int random = GetRandomInt(g_cvGiftCashMin.IntValue, g_cvGiftCashMax.IntValue);
                    g_iSurvivorCash[client] += random;
                    PrintToChatAll("\x05[提示] \x03%N \x05开启礼盒,获得了 \x03%d \x05金币.", client, random);
                    if(g_iSurvivorCash[client] >= g_cvCashMaxLimit.IntValue)
                    {
                        g_iSurvivorCash[client] = g_cvCashMaxLimit.IntValue;
                        PrintToChat(client, "\x05[提示] \x03你的金币已达上限，请尽快使用掉.");
                    }
                    AcceptEntityInput(gift, "kill");
                    break;
                }
            }
        }
        return Plugin_Continue;
    }
    return Plugin_Stop;
}

void RotateAdvance(int index, float value, int axis)
{
    if (IsValidEntity(index))
    {
        float Rotate[3];
        GetEntPropVector(index, Prop_Data, "m_angRotation", Rotate);
        Rotate[axis] += value;
        TeleportEntity(index, NULL_VECTOR, Rotate, NULL_VECTOR);
    }
}

void CheatCommand(int Client, const char[] command, const char[] arguments)
{
    if (!Client) return;
    int admindata = GetUserFlagBits(Client);
    SetUserFlagBits(Client, ADMFLAG_ROOT);
    int flags = GetCommandFlags(command);
    SetCommandFlags(command, flags & ~FCVAR_CHEAT);
    FakeClientCommand(Client, "%s %s", command, arguments);
    SetCommandFlags(command, flags);
    SetUserFlagBits(Client, admindata);
}

bool IsValidClient(int client)
{
    if (client < 1 || client > MaxClients) return false;
    if (!IsClientConnected(client)) return false;
    if (!IsClientInGame(client)) return false;
    return true;
}